<template>
  <border
    class="park-service-system"
    :moduleType="borderConfig.moduleType"
    :icon="borderConfig.icon"
    :titleText="borderConfig.titleText"
    :goPageUrl="borderConfig.goPageUrl">
    <div class="module-content" slot="module-content">
      <div class="content-top">
        <div class="top-li" v-for="(item, index) in topMessage.title" :key="index">
          <i class="icon"></i>
          <span class="title">{{item}}</span>
          <span class="message">{{topMessage.message[index]}}</span>
        </div>
      </div>
      <div class="content-bottom">
        <div class="bottom-li" v-for="(item, index) in pieConfig.pieData" :key="index">
          <pie :ref="pieConfig.ref[index]"
               :isUseValue="pieConfig.isUseValue"
               :value="item"
               :pieLegend="pieConfig.legend"
               :limitLength="pieConfig.limitLength"
               :pieType="pieType"
               :notOther="pieConfig.notOther"
               :labelShow="pieConfig.labelShow"
               :seriesRadius="seriesRadius"
               :dtottedLine="pieConfig.dtottedLine"
               :isUseComputedLine="pieConfig.isUseComputedLine"></pie>
          <span class="pie-message">{{pieConfig.title[index]}}</span>
        </div>
        <div class="legend">
          <div class="legend-li" v-for="(item, index) in pieConfig.legend" :key="index" :class="{'hide-legend-li': index > 5}">
            <div class="legend-li-content"  v-if="index < pieConfig.limitLength - 1">
              <i :style="{'background': pieConfig.pieColor[index]}"></i>
              <span>{{ item.length > 4 ? item.substring(0, 4) : item }}</span>
            </div>
            <div class="legend-li-content" v-else-if="index == pieConfig.limitLength - 1">
              <i :style="{'background': pieConfig.pieColor[index]}"></i>
              <span>{{ pieConfig.legend.length === 5 ?item.length > 4 ? item.substring(0, 4) : item : '其他' }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </border>
</template>

<script>
import Border from './../components/Border.vue'
import Pie from './../components/Pie.vue'
export default {
  name: 'parkServiceSystem',
  components: {
    Border,
    Pie
  },
  data () {
    return {
      borderConfig: {
        moduleType: '1x1',
        icon: '',
        titleText: '园区服务体系',
        goPageUrl: ''
      },
      topMessage: {
        title: ['园区服务项目数', '本年订单量', '本月订单量'],
        message: [0, 0, 0, 0, 0]
      },
      pieConfig: {
        legend: [],
        pieColor: ['#e9e703', '#e8605e', '#19dc7c', '#319ced', '#fa9022', '#8c70f8', '#cadfea', '#f59233'],
        pieData: [[], []],
        limitLength: 5,
        notOther: 'notOther',
        isUseValue: true,
        labelShow: false,
        isUseComputedLine: true,
        dtottedLine: false,
        ref: ['ref1', 'ref2'],
        title: ['服务项目分布', '订单分布']
      },
      refreshInterval: 0
    }
  },
  computed: {
    pieType () {
      return this.$store.state.themeConfig.echartsTheme === 'default' ? '' : 'default'
    },
    seriesRadius () {
      return this.$store.state.themeConfig.echartsTheme === 'default' ? [['15%', '50%'], ['5%', '10%'], ['55%', '56%'], ['62%', '63%'], ['69%', '70%']] : [['40%', '70%']]
    },
    organId () {
      return this.$store.state.organId
    },
    isIndex () {
      return this.$store.state.isIndex
    }
  },
  methods: {
    showTips  (data) {
      this.$parent.showTips(data)
    },
    getData (type, refreshInterval, isRefresh) {
      if (type === 'init') {
        this.parkApi.businessServiceSummary({ organId: this.organId }).then(res => {
          if (res.data.code === '0') {
            let data = res.data.data
            this.apiData = data
            this.topMessage.message = [data.businessServiceNum, data.curYearNum, data.curMonthNum]
          } else {
            this.apiData = {}
            this.topMessage.message = [0, 0, 0, 0, 0]
            this.$parent.showTips('请求失败，点击确认继续刷新')
          }
          if (!isRefresh) {
            this.handleData()
          }
        }, res => {
          this.apiData = {}
          this.topMessage.message = [0, 0, 0, 0, 0]
          this.$parent.showTips('请求失败，点击确认继续刷新')
        })
        if (refreshInterval) {
          this.refreshInterval = refreshInterval
          this.automaticRedrawing()
        }
      } else {
        this.handleData()
      }
    },
    handleData () {
      let data = this.apiData
      let legend = []
      let pieData1 = []
      let pieData2 = []
      if (JSON.stringify(data) !== '{}') {
        data.businessServiceDistributed.forEach((item, index) => {
          legend.push(data.businessServiceDistributed[index].name)
          pieData1.push(parseInt(data.businessServiceDistributed[index].value))
          if (data.orderDistributed[index]) {
            pieData2.push(parseInt(data.orderDistributed[index].value))
          }
        })
      }
      this.pieConfig.legend = legend
      this.pieConfig.pieData = [pieData1, pieData2]
    },
    resizeChart () {
      this.$refs.ref1[0].resizeChart()
      this.$refs.ref2[0].resizeChart()
    },
    // 根据接口返回的时间间隔请求接口
    automaticRedrawing () {
      clearInterval(window.parkServiceSystemTimer)
      if (this.refreshInterval > 0 && this.isIndex) {
        window.parkServiceSystemTimer = setInterval(() => {
          this.getData('init', '', true)
        }, this.refreshInterval * 1000)
      }
    }
  },
  mounted () {
  },
  destroyed () {
    clearInterval(window.parkServiceSystemTimer)
  }
}
</script>
<style lang="scss" scoped>
  .park-service-system {
    .module-content {
      width: 100%;
      height: 100%;
      .content-top {
        width: 100%;
        height: 130px;
        .top-li {
          width: 100%;
          height: 33px;
          box-sizing: border-box;
          padding: 0 30px;
          line-height: 33px;
          margin-bottom: 10px;
          background: url("./../../assets/images/module/bg_list.png") no-repeat center;
          background-size: 100% 100%;
          &:first-of-type {
            background: none;
            .title {
              color: #fff;
            }
            .icon {
              width: 8px;
              height: 8px;
              border: none;
              background: #fff;
              transform-origin: 50% 50%;
              transform: rotate(45deg);
              margin: 12px 15px 0 0;
            }
          }
          .icon {
            float: left;
            width: 0;
            height: 0;
            display: block;
            background: none;
            border-width: 6px;
            border-style: solid;
            border-color: transparent transparent transparent #fff;
            margin: 9px 15px 0 0;
          }
          .title {
            font-size: 12px;
            color: #72ACEC;
            float: left;
          }
          .message {
            font-size: 15px;
            color: #fff;
            float: right;
          }
        }
      }
      .content-bottom {
        width: 100%;
        height: calc(100% - 130px);
        .legend {
          width: 100px;
          height: 100%;
          display: flex;
          align-items: center;
          align-content: center;
          justify-items: center;
          flex-wrap: wrap;
          float: left;
          .legend-li {
            width: 100%;
            padding: 1px 0 1px 5px;
            overflow: hidden;
            text-align: left;
            &.hide-legend-li {
              display: none;
            }
            &:last-of-type {
              margin: 0;
            }
            i {
              display: block;
              float: left;
              width: 12px;
              height: 12px;
              vertical-align: top;
              margin: 1px 5px 0 0;
            }
            span {
              font-size: 12px;
              color: #A4B3C7;
              line-height: 14px;
              display: inline-block;
            }
          }
        }
        .bottom-li {
          width: calc((100% - 100px) / 2);
          height: 100%;
          float: left;
          position: relative;
          .pie {
            width: 100%;
            height: calc(100% - 24px);
          }
          .pie-message {
            position: absolute;
            text-align: center;
            width: 100%;
            bottom: 4px;
            display: block;
            height: 24px;
            line-height: 24px;
            color: #fff;
            font-size: 12px;
          }
        }
      }
    }
  }
  @media screen and (min-width: 1890px) {
    .park-service-system {
      .module-content {
        .content-top {
          height: 150px;
          .top-li {
            height: 40px;
            line-height: 40px;
            &:first-of-type {
              .icon {
                margin: 16px 15px 0 0;
              }
            }
            .icon {
              margin: 13px 15px 0 0;
            }
          }
        }
        .content-bottom {
          height: calc(100% - 150px);
          .legend {
            .legend-li {
              padding: 3px 0 3px 5px;
            }
          }
        }
      }
    }
  }
</style>
